<template>
  <div class="container">
    <div class="title">
      <span>添加</span>
      <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
    </div>
    <el-divider></el-divider>
    <el-form label-width="180px">
      <div class="wrap">
        <el-form-item label="产品名称" prop="name">
          <el-input
              size="medium"
              v-model="name"
              placeholder="请填写产品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <upload-imgs
              ref="uploadEle"
              :value="imgValue"
              :rules="rules"
              :multiple="true"
              :min-num="1"
              :max-num="1"
          />
        </el-form-item>
        <el-form-item label="排序" prop="order_sort">
          <el-input
              size="medium"
              type="number"
              v-model="order_sort"
              placeholder="请填写排序(值越大越在前)"
          ></el-input>
        </el-form-item>
        <el-form-item label="最多购买数量" prop="max_num">
          <el-input
              size="medium"
              type="number"
              v-model="max_num"
              placeholder="最多购买数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="销量" prop="sales_volume">
          <el-input
              size="medium"
              type="number"
              v-model="sales_volume"
              placeholder="请填写销量"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属店面" prop="store_id">
          <el-select v-model="store_id" clearable filterable placeholder="请选择店面">
            <el-option
                v-for="item in storeData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原价" prop="original_price">
          <el-input
              size="medium"
              type="number"
              v-model="original_price"
              placeholder="原价"
          ></el-input>
        </el-form-item>
        <el-form-item label="秒杀价" prop="present_price">
          <el-input
              size="medium"
              type="number"
              v-model="present_price"
              placeholder="秒杀价"
          ></el-input>
        </el-form-item>
        <el-form-item label="好友分享价" prop="new_user_price">
          <el-input
              size="medium"
              type="number"
              v-model="new_user_price"
              placeholder="好友分享价"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工奖励" prop="invite_cash">
          <el-input
              size="medium"
              type="number"
              v-model="invite_cash"
              placeholder="员工奖励"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工复购返现" prop="invite_cash_two">
          <el-input
              size="medium"
              type="number"
              v-model="invite_cash_two"
              placeholder="员工复购返现"
          ></el-input>
        </el-form-item>
        <el-form-item label="团员奖励" prop="group_invite_cash">
          <el-input
              size="medium"
              type="number"
              v-model="group_invite_cash"
              placeholder="团员奖励"
          ></el-input>
        </el-form-item>
        <el-form-item label="老顾客分享返现" prop="new_user_invite_cash">
          <el-input
              size="medium"
              type="number"
              v-model="new_user_invite_cash"
              placeholder="老顾客分享返现"
          ></el-input>
        </el-form-item>
        <el-form-item label="老板返现" prop="boss_cash">
          <el-input
              size="medium"
              type="number"
              v-model="boss_cash"
              placeholder="老板返现"
          ></el-input>
        </el-form-item>
        <el-form-item label="开始使用时间" prop="start_time">
          <el-date-picker
              v-model="start_time"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="开始使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束使用时间" prop="end_time">
          <el-date-picker
              v-model="end_time"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="结束使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产品详情">
          <div class="lin-wrap">
            <tinymce :defaultContent="desc" @change="change" />
          </div>
        </el-form-item>
        <el-form-item label="下单返优惠券">
          <el-radio v-model="coupon_status" label="0">无</el-radio>
          <el-radio v-model="coupon_status" label="1">有</el-radio>
        </el-form-item>
        <el-form-item label="优惠券名称" prop="name" v-if="coupon_status === '1'">
          <el-input
              size="medium"
              v-model="coupon.name"
              placeholder="优惠券名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="优惠券满额-得到" prop="amount_of_money" v-if="coupon_status === '1'">
          <el-input
              size="medium"
              type="number"
              v-model="coupon.amount_of_money"
              placeholder="优惠券满额(用户支付产品到满额后才可以获得优惠券)"
          ></el-input>
        </el-form-item>
        <el-form-item label="优惠券满额-使用" prop="amount_of_money_use" v-if="coupon_status === '1'">
          <el-input
              size="medium"
              type="number"
              v-model="coupon.amount_of_money_use"
              placeholder="优惠券满额(用户支付产品得到优惠券后支付金额到满额后才可使用)"
          ></el-input>
        </el-form-item>
        <el-form-item label="优惠券金额" prop="money" v-if="coupon_status === '1'">
          <el-input
              size="medium"
              type="number"
              v-model="coupon.money"
              placeholder="优惠券金额"
          ></el-input>
        </el-form-item>
        <el-form-item label="优惠券使用次数" prop="usage_times" v-if="coupon_status === '1'">
          <el-input
              size="medium"
              type="number"
              v-model="coupon.usage_times"
              placeholder="优惠券使用次数"
          ></el-input>
        </el-form-item>
        <el-form-item label="优惠券开始使用时间" prop="start_time" v-if="coupon_status === '1'">
          <el-date-picker
              v-model="coupon.start_time"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="开始使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="优惠券结束使用时间" prop="end_time" v-if="coupon_status === '1'">
          <el-date-picker
              v-model="coupon.end_time"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="结束使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item class="submit">
          <el-button type="primary" @click="createData()">保 存</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import product from '@/models/product'
import Store from '@/models/store'
import UploadImgs from '@/components/base/upload-imgs'
import Tinymce from '@/components/base/tinymce'

export default {
  components: {
    UploadImgs,
    Tinymce,
  },
  props: {
    orderSort: {
      type: Number,
    },
  },
  data() {
    return {
      imgValue: [],
      storeData: [],
      name: '',
      order_sort: '',
      max_num: '',
      sales_volume: '',
      store_id: '',
      original_price: '',
      present_price: '',
      new_user_price: '',
      invite_cash: '',
      invite_cash_two: '',
      group_invite_cash: '',
      new_user_pay_cash: '',
      new_user_invite_cash: '',
      boss_cash: '',
      start_time: '',
      end_time: '',
      desc: '',
      coupon_status: '0',
      coupon: {
        name: '',
        amount_of_money: '',
        amount_of_money_use: '',
        money: '',
        usage_times: '',
        start_time: '',
        end_time: '',
      },
      rules: {
        maxSize: 0.4,
      },
    }
  },
  mounted() {
    this.order_sort = this.orderSort
    this.getStoreAll()
  },
  methods: {
    async getStoreAll() {
      this.storeData = await Store.getStoreAll()
    },
    back() {
      this.$emit('addClose')
    },
    async createData() {
      const self = this
      const imgValue = await this.$refs.uploadEle.getValue()
      const info = {
        img_id: imgValue[0].imgId,
        name: self.name,
        order_sort: self.order_sort,
        max_num: self.max_num,
        sales_volume: self.sales_volume,
        store_id: self.store_id,
        original_price: self.original_price,
        present_price: self.present_price,
        new_user_price: self.new_user_price,
        invite_cash: self.invite_cash,
        invite_cash_two: self.invite_cash_two,
        group_invite_cash: self.group_invite_cash,
        new_user_invite_cash: self.new_user_invite_cash,
        new_user_pay_cash: self.new_user_pay_cash,
        boss_cash: self.boss_cash,
        start_time: self.start_time,
        end_time: self.end_time,
        desc: self.desc,
        coupon_status: self.coupon_status,
        coupon: {
          name: self.coupon.name,
          amount_of_money: self.coupon.amount_of_money,
          amount_of_money_use: self.coupon.amount_of_money_use,
          money: self.coupon.money,
          usage_times: self.coupon.usage_times,
          start_time: self.coupon.start_time,
          end_time: self.coupon.end_time,
        },
      }
      const res = await product.createProduct(info)
      if (res.error_code === 0) {
        self.imgValue = []
        this.$message.success(`${res.msg}`)
        this.back()
      }
    },
    change(val) {
      this.desc = val
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
